/**
 * 
 */
package py.com.alfa.escriba.gui.controller.object;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import py.com.alfa.escriba.gui.dto.admin.FuncionalidadDTO;

/**
 * @author bsandoval
 *
 */
public class PickListFuncObject {
  
  private List<FuncionalidadDTO> pickSource;
  private List<FuncionalidadDTO> pickTarget;
  
  private List<FuncionalidadDTO> pickSourceValue;
  private List<FuncionalidadDTO> pickTargetValue;
  
  public PickListFuncObject() {
    this.pickSource = new ArrayList<FuncionalidadDTO>();
    this.pickTarget = new ArrayList<FuncionalidadDTO>();
    this.pickSourceValue = new ArrayList<FuncionalidadDTO>();
    this.pickTargetValue = new ArrayList<FuncionalidadDTO>();
  }
  
  public PickListFuncObject(List<FuncionalidadDTO> pickSource) {
    this.pickSource = new ArrayList<FuncionalidadDTO>();
    this.pickSource.addAll(pickSource);
    this.pickTarget = new ArrayList<FuncionalidadDTO>();
    this.pickSourceValue = new ArrayList<FuncionalidadDTO>();
    this.pickTargetValue = new ArrayList<FuncionalidadDTO>();
  }
  
  public PickListFuncObject(List<FuncionalidadDTO> pickSource, List<FuncionalidadDTO> pickTarget) {
    this.pickSource = new ArrayList<FuncionalidadDTO>();
    this.pickSource.addAll(pickSource);
    this.pickTarget = new ArrayList<FuncionalidadDTO>();
    this.pickTarget.addAll(pickTarget);
    this.pickSourceValue = new ArrayList<FuncionalidadDTO>();
    this.pickTargetValue = new ArrayList<FuncionalidadDTO>();
  }
  
  public void add() {
    Iterator<FuncionalidadDTO> it = pickSource.iterator();
    while(it.hasNext()){
      FuncionalidadDTO next = it.next();
      if(pickSourceValue != null && pickSourceValue.contains(next)){
        getPickTarget().add(next);
        it.remove();
      }
    }    
  }

  public void addAll() {
    Iterator<FuncionalidadDTO> it = pickSource.iterator();
    while(it.hasNext()){
      FuncionalidadDTO next = it.next();
      getPickTarget().add(next);
      it.remove();
    }
  }

  public void remove() {
    Iterator<FuncionalidadDTO> it = getPickTarget().iterator();
    while(it.hasNext()){
      FuncionalidadDTO next = it.next();
      if(pickTargetValue != null && pickTargetValue.contains(next)){
        pickSource.add(next);
        it.remove();
      }
    }
    
  }

  public void removeAll() {
    Iterator<FuncionalidadDTO> it = getPickTarget().iterator();
    while(it.hasNext()){
      FuncionalidadDTO next = it.next();
      pickSource.add(next);
      it.remove();
    }
  }

  public List<Long> getSourceLongList() {
    List<Long> sources = new ArrayList<Long>();
    for (Iterator<FuncionalidadDTO> it = pickTarget.iterator(); it.hasNext();) {
      FuncionalidadDTO dto = (FuncionalidadDTO) it.next();
      sources.add(dto.getId());
    }
    return sources;
  }
  
  public List<Long> getTargetLongList() {
    List<Long> targets = new ArrayList<Long>();
    for (Iterator<FuncionalidadDTO> it = pickTarget.iterator(); it.hasNext();) {
      FuncionalidadDTO dto = (FuncionalidadDTO) it.next();
      targets.add(dto.getId());
    }
    return targets;
  }

  public List<FuncionalidadDTO> getTargetList() {
    return pickTargetValue;
  }

  /**
   * @return the pickSource
   */
  public List<FuncionalidadDTO> getPickSource() {
    return pickSource;
  }

  /**
   * @param pickSource the pickSource to set
   */
  public void setPickSource(List<FuncionalidadDTO> pickSource) {
    this.pickSource = pickSource;
  }

  /**
   * @return the pickTarget
   */
  public List<FuncionalidadDTO> getPickTarget() {
    if(pickTarget == null){
      pickTarget = new ArrayList<FuncionalidadDTO>();
    }
    return pickTarget;
  }

  /**
   * @param pickTarget the pickTarget to set
   */
  public void setPickTarget(List<FuncionalidadDTO> pickTarget) {
    this.pickTarget = pickTarget;
  }

  /**
   * @return the pickSourceValue
   */
  public List<FuncionalidadDTO> getPickSourceValue() {
    return pickSourceValue;
  }

  /**
   * @param pickSourceValue the pickSourceValue to set
   */
  public void setPickSourceValue(List<FuncionalidadDTO> pickSourceValue) {
    this.pickSourceValue = pickSourceValue;
  }

  /**
   * @return the pickTargetValue
   */
  public List<FuncionalidadDTO> getPickTargetValue() {
    return pickTargetValue;
  }

  /**
   * @param pickTargetValue the pickTargetValue to set
   */
  public void setPickTargetValue(List<FuncionalidadDTO> pickTargetValue) {
    this.pickTargetValue = pickTargetValue;
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("PickListObject [pickSource=");
    builder.append(pickSource);
    builder.append(", pickTarget=");
    builder.append(pickTarget);
    builder.append(", pickSourceValue=");
    builder.append(pickSourceValue);
    builder.append(", pickTargetValue=");
    builder.append(pickTargetValue);
    builder.append("]");
    return builder.toString();
  }
  
}
